package com.kool.kadmin.client;

import com.alibaba.fastjson.JSON;
import com.kool.kadmin.client.request.*;
import com.kool.kadmin.common.constant.ErrorCode;
import com.kool.kadmin.common.exception.AppException;
import com.kool.kadmin.common.util.HttpUtil;
import com.kool.kadmin.common.util.ResultUtil;
import com.kool.kadmin.common.util.ValidateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author luyu
 * @date 2021-04-16 15:00
 */
@Component
@Slf4j
public class KauthClient {
    @Value("${kauth.url}")
    private String urlPrefix;

    /**
     * 新增或更新用户
     *
     * @param request
     */
    public void setUser(SetUserReq request) {
        ValidateUtil.valid(request);
        String url = urlPrefix + "/kauth/setUser";
        try {
            String response = HttpUtil.doPost(url, JSON.toJSONString(request), null, 5000, 5000);
            ResultUtil.unpackData(response);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new AppException(ErrorCode.CLIENT_ERROR, "kauth", e.getMessage());
        }
    }

    /**
     * 删除用户
     *
     * @param request
     */
    public void deleteUser(DeleteUserReq request) {
        ValidateUtil.valid(request);
        String url = urlPrefix + "/kauth/deleteUser";
        try {
            String response = HttpUtil.doPost(url, JSON.toJSONString(request), null, 5000, 5000);
            ResultUtil.unpackData(response);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new AppException(ErrorCode.CLIENT_ERROR, "kauth", e.getMessage());
        }
    }

    /**
     * 授权主题
     *
     * @param request
     */
    public void topicPermit(TopicPermitReq request) {
        ValidateUtil.valid(request);
        String url = urlPrefix + "/kauth/topicPermit";
        try {
            String response = HttpUtil.doPost(url, JSON.toJSONString(request), null, 5000, 5000);
            ResultUtil.unpackData(response);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new AppException(ErrorCode.CLIENT_ERROR, "kauth", e.getMessage());
        }
    }

    /**
     * 同步用户
     *
     * @param request
     */
    public void syncMqttUsers(SyncMqttUsersReq request) {
        String url = urlPrefix + "/kauth/syncMqttUsers";
        try {
            String response = HttpUtil.doPost(url, JSON.toJSONString(request), null, 5000, 5000);
            ResultUtil.unpackData(response);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new AppException(ErrorCode.CLIENT_ERROR, "kauth", e.getMessage());
        }
    }

    /**
     * 同步主题权限
     *
     * @param request
     */
    public void syncTopicPermits(SyncTopicPermitsReq request) {
        String url = urlPrefix + "/kauth/syncTopicPermits";
        try {
            String response = HttpUtil.doPost(url, JSON.toJSONString(request), null, 5000, 5000);
            ResultUtil.unpackData(response);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new AppException(ErrorCode.CLIENT_ERROR, "kauth", e.getMessage());
        }
    }
}
